package com.cuit.mimall.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

/**
 * 统一返回结果类
 *
 * @param <T>
 * @author star
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Setter
public class R<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示信息，如果有错误时，前端可以获取该字段进行提示
     */
    private String msg;
    /**
     * 查询到的结果数据，
     */
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }


    public static <T> R<T> ok() {
        return new R<>(200, "success");
    }

    public static <T> R<T> ok(T data) {
        return new R<>(200, "success", data);
    }

    public static <T> R<T> ok(String msg) {
        return new R<>(200, msg);
    }

    public static R<?> ok(Integer code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> ok(String string, T data) {
        return new R<>(200, string, data);
    }

    public static <T> R<T> fail() {
        return new R<>(500, "fail");
    }

    public static <T> R<T> fail(String msg) {
        return new R<>(500, msg);
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> error(String msg) {
        return new R<>(500, msg);
    }


}
